﻿using System.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Sustainalytics.Utils;

namespace Sustainalytics.ReportGenerator.BL
{
    public class ControversiesChart
    {
        public SeriesPoints Controversies { get; set; }


        public byte[] GetChartImage(int width, int height)
        {
            using (var chartTemplate = new ControversiesChartTemplate(width, height))
            {
                chartTemplate.Size = new Size(width, height);

                if (Controversies != null)
                {
                    chartTemplate.Series[0].Points.Clear();

                    for (var i = 0; i < Controversies.XValues.Count(); i++)
                    {
                        var dataPoint = new Dundas.Charting.WinControl.DataPoint
                        {
                            XValue = Controversies.XValues[i],
                            YValues = new[] {Controversies.YValues[i], 0},
                            AxisLabel = Controversies.AxisLabels[i],
                            Color = Controversies.XValues[i].NearlyEq(Controversies.XValue)
                                ? Color.FromArgb(249, 178, 96)
                                : Color.FromArgb(166, 166, 166)
                        };

                        chartTemplate.Series[0].Points.Add(dataPoint);
                    }
                }

                // Create new bitmap where chart will be printed
                using (var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb))
                {
                    // Create graphics object from bitmap.
                    using (var imageGraphics = Graphics.FromImage(bitmap))
                    {
                        // Print chart to the bitmap object.
                        var rectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                        chartTemplate.Printing.PrintPaint(imageGraphics, rectangle);
                    }

                    using (var stream = new MemoryStream())
                    {
                        bitmap.Save(stream, ImageFormat.Png);
                        var buffer = stream.ToArray();
                        return buffer;
                    }
                }
            }
        }

        public Dundas.Charting.WinControl.Chart ChartTemplateOld(int width, int height)
        {
            //Create a new Chart object and modify the properties at the root level
            Dundas.Charting.WinControl.Chart chartObj = new Dundas.Charting.WinControl.Chart();

            chartObj.Palette = Dundas.Charting.WinControl.ChartColorPalette.Dundas;
            chartObj.Size = new Size(width, height);
            chartObj.BackGradientEndColor = Color.Transparent;
            chartObj.BorderLineColor = Color.Transparent;
            chartObj.BorderLineWidth = 0;
            chartObj.AlwaysRecreateHotregions = true;


            //Create a new ChartArea and add it to the ChartAreas Collection
            Dundas.Charting.WinControl.ChartArea defaultChartArea = new Dundas.Charting.WinControl.ChartArea();
            defaultChartArea.BackColor = Color.Transparent; 
            defaultChartArea.BackGradientEndColor = Color.Transparent; 
            defaultChartArea.ShadowColor = Color.Transparent; 
            defaultChartArea.BorderColor = Color.FromArgb(64, 64, 64, 64);
            defaultChartArea.BorderStyle = Dundas.Charting.WinControl.ChartDashStyle.NotSet;
            defaultChartArea.Name = "Default";
            chartObj.ChartAreas.Add(defaultChartArea);

            //Modify the settings for the ChartArea named "ChartArea"
            defaultChartArea.AxisY.LineColor = Color.Black;
            defaultChartArea.AxisY.LineWidth = 1;
            defaultChartArea.AxisY.LabelsAutoFit = true;
            defaultChartArea.AxisY.LabelsAutoFitMinFontSize = 10;
            defaultChartArea.AxisY.StartFromZero = true;
            defaultChartArea.AxisY.Minimum = 0;
            defaultChartArea.AxisY.Maximum = 100;
            defaultChartArea.AxisY.MajorGrid.Interval = 20;
            defaultChartArea.AxisY.MajorGrid.LineColor = Color.Black;
            defaultChartArea.AxisY.MajorGrid.LineWidth = 1;
            defaultChartArea.AxisY.MajorTickMark.Enabled = false;
            defaultChartArea.AxisY.MajorTickMark.Interval = 20;
            defaultChartArea.AxisY.MajorTickMark.IntervalOffsetType = Dundas.Charting.WinControl.DateTimeIntervalType.NotSet;
            defaultChartArea.AxisY.MajorTickMark.IntervalType = Dundas.Charting.WinControl.DateTimeIntervalType.Number;
            defaultChartArea.AxisY.LabelStyle.Enabled = true;
            defaultChartArea.AxisY.LabelStyle.Interval = 20;
            defaultChartArea.AxisY.LabelStyle.Font = new Font("Calibri", 10, FontStyle.Regular, GraphicsUnit.Point, (byte)1, false);
            defaultChartArea.AxisY.LabelStyle.Format = "P0";

            //Modify the settings for the ChartArea named "ChartArea"
            defaultChartArea.AxisX.LineColor = Color.Black;
            defaultChartArea.AxisX.LineWidth = 1;
            defaultChartArea.AxisX.LabelsAutoFit = false;
            defaultChartArea.AxisX.TitleFont = new Font("Calibri", 10, FontStyle.Regular, GraphicsUnit.Point, (byte)1, false);
            defaultChartArea.AxisX.StartFromZero = false;
            defaultChartArea.AxisX.Minimum = -1;
            defaultChartArea.AxisX.Maximum = 6;
            defaultChartArea.AxisX.MajorGrid.Enabled = false;
            defaultChartArea.AxisX.MajorGrid.Interval = 1;
            defaultChartArea.AxisX.MajorGrid.IntervalOffsetType = Dundas.Charting.WinControl.DateTimeIntervalType.NotSet;
            defaultChartArea.AxisX.MajorGrid.IntervalType = Dundas.Charting.WinControl.DateTimeIntervalType.Number;
            defaultChartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
            defaultChartArea.AxisX.MajorTickMark.Enabled = false;
            defaultChartArea.AxisX.MajorTickMark.Style = Dundas.Charting.WinControl.TickMarkStyle.Inside;
            defaultChartArea.AxisX.MajorTickMark.Interval = 5;
            defaultChartArea.AxisX.MajorTickMark.IntervalOffsetType = Dundas.Charting.WinControl.DateTimeIntervalType.Number;
            defaultChartArea.AxisX.MajorTickMark.IntervalType = Dundas.Charting.WinControl.DateTimeIntervalType.Number;
            defaultChartArea.AxisX.LabelStyle.Enabled = true;
            defaultChartArea.AxisX.LabelStyle.Font = new Font("Calibri", 10, FontStyle.Regular, GraphicsUnit.Point, (byte)1, false);
            defaultChartArea.AxisX.LabelStyle.FontAngle = -45;

            //Modify the settings for the ChartArea named "ChartArea"
            defaultChartArea.Position.X = 0;
            defaultChartArea.Position.Y = 0;
            defaultChartArea.Position.Width = 100;
            defaultChartArea.Position.Height = 100;
            defaultChartArea.Position.Auto = true;

            //Modify the settings for the ChartArea named "ChartArea"
            defaultChartArea.InnerPlotPosition.Width = 100;
            defaultChartArea.InnerPlotPosition.Height = 100;
            defaultChartArea.InnerPlotPosition.Auto = true;


            //Create a new Series and add it to the Series Collection
            Dundas.Charting.WinControl.Series series1Series = new Dundas.Charting.WinControl.Series();
            series1Series.CustomAttributes = "PointWidth=0.5, ColumnLabelStyle=Horizontal, DrawingStyle=Default, LabelStyle=Auto, DrawSideBySide=Auto";
            series1Series.Name = "Series1";
            series1Series.Color = Color.Transparent;
            series1Series.BorderColor = Color.Transparent;
            series1Series.BorderWidth = 0;
            series1Series.Font = new Font("Calibri", 10, FontStyle.Regular, GraphicsUnit.Point, (byte)1, false);
            series1Series.MarkerStyle = Dundas.Charting.WinControl.MarkerStyle.None;
            series1Series.MarkerColor = Color.Silver;
            series1Series.MarkerSize = 10;
            series1Series.XValueType = Dundas.Charting.WinControl.ChartValueTypes.Int;
            series1Series.YValueType = Dundas.Charting.WinControl.ChartValueTypes.Int;
            series1Series.YValuesPerPoint = 2;
            series1Series.PaletteCustomColors = new Color[] { };
            series1Series.ShadowOffset = 0;
            series1Series.ShowLabelAsValue = false;
            series1Series.Font = new Font("Calibri", 10, FontStyle.Regular, GraphicsUnit.Point, (byte)1, false);
            series1Series.ShowInLegend = false;
            series1Series.LegendText = "Series1";
            chartObj.Series.Add(series1Series);

            //Create a new DataPoint and add it to the Series.Points Collection
            Dundas.Charting.WinControl.DataPoint dataPoint1 = new Dundas.Charting.WinControl.DataPoint();
            dataPoint1.XValue = 1;
            dataPoint1.YValues = new double[] { 0, 0 };
            dataPoint1.AxisLabel = "35-40";
            dataPoint1.Font = new Font("Calibri", 10F, FontStyle.Regular, GraphicsUnit.Point, (byte)1, false);
            series1Series.Points.Add(dataPoint1);

            return chartObj;
        }

    }
}
